package com.snap.vseries.adapter;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.snap.vseries.R;
import com.snap.vseries.base.OnItemClickListener;
import com.snap.vseries.bean.TestItem;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.internal.DebouncingOnClickListener;

public class CitAdapter extends RecyclerView.Adapter<CitAdapter.Holder> {

    private List<TestItem> testItems;
    private OnItemClickListener onItemClickListener;

    public CitAdapter(List<TestItem> testItems, OnItemClickListener onItemClickListener) {
        this.testItems = testItems;
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return bindClick(new Holder(
                LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cit, parent, false)));
    }

    private Holder bindClick(Holder holder) {
        holder.itemView.setOnClickListener(new DebouncingOnClickListener() {
            @Override
            public void doClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(CitAdapter.this, v, holder.getLayoutPosition());
                }
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
//        if (position == 0) {
//            holder.nameTv.setText("自动测试");
//            holder.resultTv.setVisibility(View.GONE);
//        } else {
        TestItem item = testItems.get(position);
        holder.nameTv.setText(item.getName());
        holder.itemView.setBackgroundColor(item.isChecked() ? 0x7f888888 : Color.WHITE);
        holder.resultTv.setTextColor(item.getState() == 2 ? Color.RED : Color.GREEN);
        String resStr = "";
        if (item.getState() == 1) resStr = "通过";
        else if (item.getState() == 2) resStr = "失败";
        holder.resultTv.setText(resStr);
//        }
    }

    @Override
    public int getItemCount() {
        return testItems == null ? 0 : testItems.size();
    }

    class Holder extends RecyclerView.ViewHolder {

        @BindView(R.id.name_tv)
        TextView nameTv;
        @BindView(R.id.result_tv)
        TextView resultTv;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
